//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "pseudo.h"

static IDeviceDriver    * g_pHackHelper   = NULL;
EXTERN ECode GetLocalGrafixEngine(PInterface * ppGfxEngine);
EXTERN ECode GetHackHelper(IDeviceDriver * * ppHacker);

EXTERN ECode HackGrafixRunningObject(
            WString name,
            PInterface * ppService,
            Boolean * pbFiltered)
{
    ECode ec = NOERROR;

    assert(NULL != ppService);
    assert(NULL != pbFiltered);

    *pbFiltered = TRUE;
    *ppService  = NULL;

    if (!name.Compare(L"Grafix")) {
        return GetLocalGrafixEngine(ppService);
    }

    if (NULL == g_pHackHelper) {
        ec = GetHackHelper(&g_pHackHelper);
        if (FAILED(ec)) {
            return ec;
        }
    }

    IProcess * pProcess = NULL;
    ProcessId  processId ;

    CPseudoKernel::Process_GetCurrent(&pProcess);
    pProcess->GetId(&processId);
    MemoryBuf_<sizeof(ProcessId)>  inBuf;
    inBuf.Copy((Byte *)&processId, sizeof(ProcessId));
    MemoryBuf_<sizeof(PInterface)> outBuf;

    if (!name.Compare(L"device:keypad0")) {
        ec = g_pHackHelper->Control(0, inBuf, &outBuf, NULL);
        if (FAILED(ec)) {
            return ec;
        }
        Address address = *(Address *)outBuf.GetPayload();
        *ppService = (PInterface)address;
       // *ppService = (PInterface)outBuf.GetPayload();
    }
    else if (!name.Compare(L"device:touch")) {
        ec = g_pHackHelper->Control(1, inBuf, &outBuf, NULL);
        if (FAILED(ec)) {
             return ec;
        }
        Address address = *(Address *)outBuf.GetPayload();
        *ppService = (PInterface)address;
    }
    else if (!name.Compare(L"device:display0")) {
        ec = g_pHackHelper->Control(2, inBuf, &outBuf, NULL);
        if (FAILED(ec)) {
            return ec;
        }
        Address address = *(Address *)outBuf.GetPayload();
        *ppService = (PInterface)address;
    }
    else if (!name.Compare(L"device:nc0")) {
        ec = g_pHackHelper->Control(3, inBuf, &outBuf, NULL);
        if (FAILED(ec)) {
            return ec;
        }
        Address address = *(Address *)outBuf.GetPayload();
        *ppService = (PInterface)address;
    }
	else if (!name.Compare(L"device:ime")) {
        ec = g_pHackHelper->Control(4, inBuf, &outBuf, NULL);
        if (FAILED(ec)) {
            return ec;
        }
        Address address = *(Address *)outBuf.GetPayload();
        *ppService = (PInterface)address;
    }
    else {
        *pbFiltered = FALSE;
    }

    return ec;
}

